package com.kyzh.core.adapters.v3

import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.GroupChatItem
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.GameRequest
import com.kyzh.core.R
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.ItemGroupchatBinding
import com.kyzh.core.utils.onLoginClick
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast


class HomeGroupChatListAdapter(layout:Int,beans:ArrayList<GroupChatItem>): BaseQuickAdapter<GroupChatItem, BaseDataBindingHolder<ItemGroupchatBinding>>(layout,beans){

    override fun convert(holder: BaseDataBindingHolder<ItemGroupchatBinding>, item: GroupChatItem) {
        holder.dataBinding?.let {
            it.data = item
            it.btJoin.onLoginClick(context) {
                if (item.join !=1){
                    GameRequest.addGroup(item.id){
                        item.join=1
                        item.url=this
                        notifyDataSetChanged()
                    }
                }else{
                    context.startActivity<BrowserActivity>(GlobalKeys.LINK to item.url ,GlobalKeys.TITLE to item.name)
                }
            }
            it.btJoin.text = context.getString(if (item.join==1) R.string.joined else R.string.join)
            it.btJoin.background = if (item.join==1) null else ContextCompat.getDrawable(context, R.drawable.button_bg )
            it.btJoin.setTextColor(ContextCompat.getColor(context,if (item.join==1) R.color.colorPrimary else R.color.white ))

        }

        holder.itemView.setOnClickListener {
            if (item.join ==1){
                context.startActivity<BrowserActivity>(GlobalKeys.LINK to item.url ,GlobalKeys.TITLE to item.name)
            }else{
                context.getString(R.string.pleaseJoinGroup).toast()
            }
        }
    }
}